$(function(){  	
  	initRoles(); 
});
/**
 * 初始化角色列表
 */
function initRoles(){
	dataTable({
      tableId:'roles',
      title:'角色列表',
      url:'role.do?method=queryRole',
      idField:'roleId',
      columns:[[
		 {field : 'roleId',checkbox : true,align : 'center'},
		 {title : '角色名称',field : 'roleName',width : 200},
		 {title : '描述',field : 'remarks',width : 300}		 
		 ]],
	  showPage:true,
	  toolbar:'toolbar'
   });
}
$("#role_add").click(function(){
	$("#roleWin").window('setTitle',"添加角色").window('open');	
    $("#roleForm").form('clear');    
});
//修改数据
$("#role_update").click(function(){
   var rows = $("#roles").datagrid("getChecked");
   var len = rows.length;
   if(len<1){
   	  $.messager.alert("系统提示", "请选择要修改的数据！","warning");
   	  return;
   }else if(len>1){
   	  $.messager.alert("系统提示", "每次只能修改一条数据，请重新选择！","warning");
   	  return;
   }
   $("#roleWin").window('setTitle',"修改角色").window('open');	
   $("#roleForm").form('clear'); 
   $("#roleForm").form('load',{roleName:rows[0].roleName,roleId:rows[0].roleId,remarks:rows[0].remarks}); 
});
$("#add_save").click(function(){
   var roleId = $("#roleId").val();
   if(isEmpty(roleId)){
      submitForm({
       formId:'roleForm',
       url:'role.do?method=addRole',
       callback:callback_add
     });
   }else{
       submitForm({
        formId:'roleForm',
        url:'role.do?method=updateRole',
        callback:callback_update
      });   
   }
});
//保存菜单后的回调方法
function callback_add(data){    
   if(data=="SUCCESS"){   	   	  
   	  $.messager.confirm("系统提示","角色信息增加成功，是否继续添加？",reloadData);
   }else if(data=="FAIL"){   	  
   	  $.messager.alert("系统提示","角色信息增加失败，请重试！","error");
   }else{
   	  $.messager.alert("系统提示","保存角色时系统异常："+data,"error");	
   }
}
//保存菜单后的回调方法
function callback_update(data){    
   if(data=="SUCCESS"){   	   	  
   	  $.messager.alert("系统提示","角色信息修改成功！","info",reload1);
   }else if(data=="FAIL"){   	  
   	  $.messager.alert("系统提示","角色信息修改失败，请重试！","error");
   }else{
   	  $.messager.alert("系统提示","保存角色时系统异常："+data,"error");	
   }
}
//刷新数据
function reloadData(r) {	
	if(r){		
	    clearValue();
	}else{	   	   
	   $('#roleWin').window('close');
	}
	reloadTable({tableId:'roles'});
}
function reload1(){	
	$('#roleWin').window('close');
	reloadTable({tableId:'roles'});
}
function clearValue(){
   $("#roleForm").form('clear');
}
//删除数据
$("#role_delete").click(function(){  
   var rows = $('#roles').datagrid('getSelections');
   var len = rows.length;
   if(len==0){
      $.messager.alert("系统提示","请选择要删除的记录","warning");
      return;
   }
   var ids = new Array();
   for(var i = 0;i<len;i++){     
   	  ids.push(rows[i].roleId);
   }
   deleteRole(ids);
});
function deleteRowData(index){
	var rows = $("#roles").datagrid("getRows");
	var roleId = rows[index].roleId;
	deleteRole(roleId);
}
//删除数据
function deleteRole(roleIds){   
   var len = 1;
   var ids = new Array();
   if(typeof(roleIds)!='string'){
     len = roleIds.length;
     ids = roleIds;
   }else{
     ids.push(roleIds);
   }
   var url = "role.do?method=deleteRole";
    //删除数据
   deleteData({
     len:len,
     url:url,
     params:{'id':ids,'version':Math.random()},
     dataType:'text',
     callback:function(msg){
        if(msg=="EMPTY"){
           $.messager.alert("系统提示","请选择要删除的记录","warning");
        }else if(msg=="FAIL"){
           $.messager.alert("系统提示","删除菜单信息失败","warning");
        }else if(msg=="SUCCESS"){
           reloadTable({tableId:'roles'});		   
        }else{
            $.messager.alert("系统提示","系统出现异常："+msg,"error");
        }
     }
   });
}
function updateRowData(index){
  var rows = $("#roles").datagrid("getRows");
  var row = rows[index];
  $("#roleWin").window('setTitle',"修改角色").window('open');	
  $("#roleForm").form('clear'); 
  $("#roleForm").form('load',{roleName:row.roleName,roleId:row.roleId,remarks:row.remarks}); 
}
$("#role_author").click(function(){
   var rows = $("#roles").datagrid("getChecked");
   var len = rows.length;
   if(len<1){
   	  $.messager.alert("系统提示", "请选择要授权的角色！","warning");
   	  return;
   }else if(len>1){
   	  $.messager.alert("系统提示", "每次只能为一个角色授权，请重新选择！","warning");
   	  return;
   }
   var url = "role.do?method=index&type=author";
   url = urlAddParmert(url,"roleId",rows[0].roleId);
   url = urlAddParmert(url,"roleName",rows[0].roleName);
   //打开easyui窗口
   easyuiWidow({
   	 title:'角色【'+rows[0].roleName+'】授权',
     winId:'authorWin',
     width:800,   
     height:400,
     iconCls:'icon-win-addmenu',
     url:url
   });
});

$("#search").click(function(){
	search();
});

function search(){
	
	var roleName = $("#roleName").val();
	var params = 
	 {  
	     "roleName":roleName
    };	
	reloadTable({tableId:'roles',params:params});
}

